package com.zhangyu.shadow.repair.delegate.dept;

import com.alibaba.fastjson.TypeReference;
import com.zhangyu.shadow.repair.base.IStringDataConvert;
import com.zhangyu.shadow.repair.config.RequestUrls;
import com.zhangyu.shadow.repair.delegate.bean.OrgDeptBean;
import com.zhangyu.shadow.repair.manager.AccountManager;

import java.util.List;

import io.reactivex.disposables.Disposable;
import st.lib.base.net.callback.IError;
import st.lib.base.net.callback.ISuccess;
import st.lib.base.net.rx.RxRestClient;
import st.lib.base.ui.recycler.ObjConverter;

/**
 * Description:
 *
 * @author Hequn.Lee
 * @date 2018/11/5
 */
public class DeptModel implements DeptContract.Model {

    @Override
    public Disposable getOrgDept(final IStringDataConvert<List<OrgDeptBean>> convert, IError error) {
        return RxRestClient.builder()
                .url(RequestUrls.URL_ORG_DEPT)
                .params(AccountManager.getInstance().getBaseInfo())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        convert.convert(convertLoginBean(response));
                    }
                })
                .error(error)
                .build()
                .post();
    }

    @Override
    public Disposable setPersonOrgDept(String personId,
                                       String deptId, String groupId,
                                       final IStringDataConvert<String> convert, IError error) {
        return RxRestClient.builder()
                .url(RequestUrls.URL_SET_DEPT_ORG)
                .params(AccountManager.getInstance().getBaseInfo())
                .params("personId", personId)
                .params("deptId", deptId)
                .params("groupId", groupId)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        convert.convert(response);
                    }
                })
                .error(error)
                .build()
                .post();
    }


    private List<OrgDeptBean> convertLoginBean(String response) {
        return new ObjConverter<List<OrgDeptBean>>().convert(response
                , new TypeReference<List<OrgDeptBean>>() {
                });
    }
}
